from constant import *


class Sun(MovableGifImageObject):
    def __init__(self, screen: pg.Surface):
        coordinate = (randint(0, LAWNS_WIDTH) + LAWN_START_X, 0)
        super().__init__(screen, 'images\\interface\\Sun.gif', frames_update_rate=6)
        self.trajectory = self.points_trajectory(coordinate, (coordinate[0], LAWNS_HEIGHT), step=2)
        self.disappear = randint(SUN_DISAPPEAR[0], SUN_DISAPPEAR[1])
        self.over = False

    def onclick(self, mouse_pos: tuple[int | float, int | float] | list[int | float, int | float]):
        if self.is_in(mouse_pos):
            self.trajectory = self.points_trajectory(self.coordinate, (0, 0), step=20)
            return True


class SunBack(ImageObject):
    def __init__(self, screen):
        super().__init__(screen, 'images\\interface\\SunBack.png')
        self.sun = 0
        self.sun_font = pg.font.Font(None, 50)
        self.sun_font_color = 'black'

    def update(self):
        text = self.sun_font.render(f'{self.sun}', True, self.sun_font_color)
        self.screen.blit(self.frame_sur, self.coordinate)
        self.screen.blit(text, (self.coordinate[0] + 100, self.coordinate[1] + 15))


class RandomSun:
    def __init__(self, screen, sun_back: SunBack):
        self.screen = screen
        self.suns: list[Sun] = []
        self.time = pg.time.get_ticks()
        self.sun_back = sun_back
        self.appear = randint(SUN_APPEAR[0], SUN_APPEAR[1]) * 1000

    def onclick(self, mouse_pos):
        for sun in self.suns:
            if sun.onclick(mouse_pos):
                return

    def update(self):
        if pg.time.get_ticks() - self.time > self.appear:
            self.appear = randint(SUN_APPEAR[0], SUN_APPEAR[1]) * 1000
            self.suns.append(Sun(self.screen))
            self.time = pg.time.get_ticks()
        self.suns = [sun for sun in self.suns if not sun.over]
        for sun in self.suns:
            if sun.coordinate[0] == 0 and sun.coordinate[1] == 0:
                self.sun_back.sun += 25
                sun.over = True
            else:
                sun.update()


class FlagMeter:
    def __init__(self, screen: pg.Surface, waves_zombies: list[int]):
        self.screen = screen
        self.coordinate = (SCREEN_WIDTH - FLAG_METER_SIZE[0], SCREEN_HEIGHT - FLAG_METER_SIZE[1])
        self.empty = pg.image.load('images\\interface\\FlagMeterEmpty.png')
        self.full = pg.image.load('images\\interface\\FlagMeterFull.png')
        self.parts1 = pg.image.load('images\\interface\\FlagMeterParts1.png')
        self.parts2 = pg.image.load('images\\interface\\FlagMeterParts2.png')
        self.waves_zombies = waves_zombies
        self.now_zombies = -1
        self.waves = len(waves_zombies)
        self.wave_length = FLAG_LENGTH / self.waves

    def update(self, wave: int, now_zombies):
        """

        :param wave: 波次，从0开始
        :param now_zombies: 现在这个波次出现的僵尸数量
        :return: None
        """
        surplus = now_zombies / self.waves_zombies[wave]
        surface = pg.Surface(((self.waves - wave - surplus) * self.wave_length + 10, FLAG_METER_SIZE[1]), pg.SRCALPHA)
        surface.blit(self.empty, (10, 25))
        for number in range(self.waves):
            x = FLAG_LENGTH - (number + 1) * self.wave_length + self.coordinate[0] + 16
            if number < wave:
                y = self.coordinate[1] + 8
            elif number == wave and surplus == 1.0:
                y = self.coordinate[1] + 8
            else:
                y = self.coordinate[1] + 10
            self.screen.blit(self.parts2, (x, y))
        self.screen.blit(self.full, (self.coordinate[0] + 10, self.coordinate[1] + 25))
        self.screen.blit(self.parts1, ((self.coordinate[0] + (self.waves - wave - surplus) * self.wave_length) + 10, self.coordinate[1] + 25))
        self.screen.blit(surface, self.coordinate)


class HP:
    def __init__(self, screen: pg.Surface, now_hp: int, total_hp: int, coordinate: tuple[int, int]):
        self.screen = screen
        self.now_hp = now_hp
        self.total_hp = total_hp
        self.coordinate = (coordinate[0], coordinate[1] - HP_SIZE[1])
        self.surface = pg.Surface(HP_SIZE, pg.SRCALPHA)
        self.font = pg.font.Font(None, 20)

    def update(self):
        text = self.font.render(f'{self.now_hp} / {self.total_hp}', True, RED)
        pg.draw.rect(self.surface, GREEN1, (0, 0, HP_SIZE[0] * self.now_hp / self.total_hp, HP_SIZE[1]))
        pg.draw.rect(self.surface, YELLOW, ((0, 0) + HP_SIZE), width=1)
        self.surface.blit(text, (10, 5))
        self.screen.blit(self.surface, self.coordinate)




